#include<iostream>
using namespace std;

#include<unistd.h>
#include<cassert>
#include<sys/types.h>
#include<sys/wait.h>
#include<cstdio>
#include<cstring>

int main()
{
    int fds[2];
    int n = pipe(fds);
    assert(n == 0);

    pid_t id = fork();
    assert(id >= 0);
    if(id == 0)
    {
        close(fds[0]);
        const char* s = "不要回答！";
        int cnt = 0;
        while(true)
        {
            char buffer[1024];
            snprintf(buffer, sizeof(buffer), "child->parent: %s[%d] id: %d", s, ++cnt, getpid());
            write(fds[1], buffer, strlen(buffer));
            sleep(1);
        }
        close(fds[1]);
        exit(0);
    }
    close(fds[1]);
    while(true)
    {
        char buffer[1024];
        ssize_t ret = read(fds[0], buffer, sizeof(buffer)-1);
        if(ret != 0) buffer[ret] = '\0';
        cout << "i am parent, i get you say: " << buffer << " | parent id: " << getpid() << endl;
    }

    int status;
    waitpid(id, &status, 0);
    close(fds[0]);
    return 0;
}